package top.wardyan.wdev.dbdevassist.service.impl;

import top.wardyan.base.service.IGetIDService;
import top.wardyan.base.service.impl.GetIDServiceImpl;
import top.wardyan.wdev.dbdevassist.model.MasterModel;
import top.wardyan.wdev.dbdevassist.service.IMasterService;

public class MasterServiceImpl<T extends MasterModel<T>> extends top.wardyan.base.service.impl.MasterServiceImpl<T> implements IMasterService<T> {

	protected IGetIDService getIDService = GetIDServiceImpl.getInstance("yp_getid");

	public MasterServiceImpl(T dao) {
		super(dao, 1);
	}
	
	

	@Override
	protected T initPrimaryKey(T obj) {
		if(obj != null && obj.primaryKeyType() > 0) {
			if(obj.primaryKeyType() == 1 && (obj.getSID() == 0)) {
				T t = this.findBySql("SELECT sID FROM " + this.table + " ORDER BY sID DESC");
				if(t != null) {
					obj.setSID(t.getSID() + 1);
				}else {
					obj.setSID(1);
				}
			}else if(obj.primaryKeyType() == 2 && (obj.getSID() == 0)) {
				obj.setSID(getIDService.exec("yp_getid", 0, this.table, 1));
			}
		}
		return obj;
	}



	@Override
	public T find(String xName) {
		return this.findBySql(this.sql + " WHERE xName = ?", xName);
	}

}
